package cz.proks.easyhelpdesk.gae.api.impl.dbo;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import cz.proks.easyhelpdesk.api.model.Ticket;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.ConvertibleDbo;
import cz.proks.easyhelpdesk.gae.api.impl.dbo.base.IdentifiableModelDbo;

public class Thread extends cz.proks.easyhelpdesk.api.model.Thread implements IdentifiableModelDbo, ConvertibleDbo<cz.proks.easyhelpdesk.api.model.Thread> {    
    private cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket entryTicket;
    private List<cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket> threadTickets;
    
    @Override
    public Long getEntryTicketId() {     
        return super.getEntryTicketId();
    }

    @Override
    public void setEntryTicketId(Long entryTicketId) {
        super.setEntryTicketId(entryTicketId);
        
        if (entryTicketId == null)
            entryTicket = null;
        else { 
            entryTicket = new cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket(); 
            entryTicket.setId(entryTicketId);
        }        
    }
                
    @Override
    public List<Ticket> getTickets() {                
        return super.getTickets();
    }

    @Override
    public void setTickets(List<Ticket> tickets) {        
        super.setTickets(tickets);
        
        this.threadTickets = new LinkedList<cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket>();
        
        if (tickets != null)
            for(Ticket domainTicket : tickets) {
                cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket dboTicket = new cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket();
                dboTicket.toDbo(domainTicket);
                threadTickets.add(dboTicket);
            }
    }        

    public List<cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket> getThreadTickets() {
        return threadTickets;
    }

    public void setThreadTickets(List<cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket> threadTickets) {
        this.threadTickets = threadTickets;
        
        List<Ticket> tickets = new LinkedList<Ticket>();
        if (threadTickets != null)
            for (cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket dboTicket : threadTickets) {
                Ticket ticket = dboTicket.asDomain();
                tickets.add(ticket);
            }
        
        super.setTickets(tickets);
    }

    public cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket getEntryTicket() {
        return entryTicket;
    }

    public void setEntryTicket(cz.proks.easyhelpdesk.gae.api.impl.dbo.Ticket entryTicket) {
        this.entryTicket = entryTicket;
        super.setEntryTicketId(entryTicket != null ? entryTicket.getId() : null); 
    }
        
    

    @Override
    public void toDbo(cz.proks.easyhelpdesk.api.model.Thread domain) {
        BeanUtils.copyProperties(domain, this);
    }

    @Override
    public cz.proks.easyhelpdesk.api.model.Thread asDomain() {
        cz.proks.easyhelpdesk.api.model.Thread thread = new cz.proks.easyhelpdesk.api.model.Thread();
        BeanUtils.copyProperties(this, thread, new String[] {"entryTicket"});
        return thread;
    }        
}
